package com.huluobo.a2211mdmdemo.video

import android.util.Log
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.staggeredgrid.LazyVerticalStaggeredGrid
import androidx.compose.foundation.lazy.staggeredgrid.StaggeredGridCells
import androidx.compose.foundation.lazy.staggeredgrid.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import com.google.gson.Gson
import com.huluobo.lib_base.RoutePath
import com.huluobo.lib_base.UIState
import com.huluobo.lib_http.Const
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.launch

/**
 *  Created by LC on 2025/5/8.
 */

@Composable
fun VideoListPage(navHostController: NavHostController, vm: VideoViewModel = hiltViewModel()) {
    val list = remember {
        SnapshotStateList<VideoEntityItem>()
    }

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect {
                when (it) {
                    is UIState.OnError -> {
                        Log.e("TagA", "获取视频列表失败:${it.msg}")
                    }

                    UIState.OnLoading -> {}
                    is UIState.OnSuccess<*> -> {
                        list.clear()
                        list.addAll(it.data as VideoEntity)
                        Log.e("TagA", "videos:${list.size}")
                    }
                }
            }
        }
        vm.sendIntent(VideoIntent.GetVideos(1, 50))
    })

    Column(Modifier.padding(20.dp)) {
        Text(text = "视频", fontSize = 26.sp, fontWeight = FontWeight.Bold)
        LazyVerticalStaggeredGrid(
            columns = StaggeredGridCells.Fixed(2), content = {
                items(list) {
                    VideoItem(entityItem = it, navHostController = navHostController)
                }
            }, verticalItemSpacing = 10.dp, horizontalArrangement = Arrangement.spacedBy(10.dp)
        )
    }
}

@Composable
fun VideoItem(entityItem: VideoEntityItem, navHostController: NavHostController) {
    Card(
        border = BorderStroke(1.dp, Color.LightGray), colors = CardDefaults.cardColors(containerColor = Color.White),
        modifier = Modifier.clickable {
            val json = Gson().toJson(entityItem)
            MMKV.defaultMMKV().encode(Const.PARAM_VIDEO_KEY, json)
            navHostController.navigate(RoutePath.VIDEO)
        }
    ) {
        Column(
            Modifier
                .padding(10.dp)
                .fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(10.dp)
        ) {
            AsyncImage(
                model = entityItem.videomainimg,
                contentDescription = "",
                modifier = Modifier
                    .fillMaxWidth(),
                contentScale = ContentScale.Crop
            )
            Row(verticalAlignment = Alignment.CenterVertically) {
                AsyncImage(
                    model = entityItem.headpath, contentDescription = "", modifier = Modifier
                        .size(40.dp)
                        .clip(
                            CircleShape
                        )
                )
                Spacer(modifier = Modifier.width(10.dp))
                Text(text = entityItem.authname)
            }
        }
    }
}